//
//  HeaderViewControllerViewController.m
//  CwinCar
//
//  Created by dlios on 15/10/16.
//  Copyright © 2015年 dlios. All rights reserved.
//

#import "HeaderViewControllerViewController.h"
#import "CCfindHeadDetailCell.h"
#import "CCTypePriceViewController.h"
#import "CCProFindModel.h"
#import <BaiduMapAPI/BMapKit.h>
#import "FindMapViewController.h"
@interface HeaderViewControllerViewController ()<UITableViewDataSource , UITableViewDelegate,BMKLocationServiceDelegate,BMKGeoCodeSearchDelegate>
{
    
    
    UIView *_headView ;
    NSInteger _proTag ;
    NSInteger _cityTag ;
    NSNumber * _cid ;
    UILabel * _cLabel;
    UILabel * _pLabel ;
    int _i  ;
    CGFloat _lat ;
    CGFloat _lon ;
}

@property (nonatomic , strong) NSMutableArray * proArray ;
@property (nonatomic , strong) NSMutableArray * cityArray ;
@property (nonatomic , strong) NSMutableArray *sectionArray ;
@property (nonatomic , strong) NSMutableArray * allCarArray;
@property (nonatomic , strong) UITableView * provinceTableView ;

@property (nonatomic , strong) UITableView * cityTableView ;

@property (nonatomic , strong) NSString * proText ;

@property (nonatomic , strong) NSString * cityText ;

@property (nonatomic , strong) NSMutableDictionary * bigAreaDictionary ;
//地图
@property (nonatomic, strong)BMKMapView * mapView;
@property (nonatomic, strong)BMKLocationService * locService;
@property (nonatomic , strong) BMKGeoCodeSearch * searcher;

@end

@implementation HeaderViewControllerViewController
-(void)viewWillAppear:(BOOL)animated{
    
    self.navigationController.navigationBar.hidden = YES;
#if 1
    //初始化BMKLocationService
    if (_locService == nil) {
        
        _locService = [[BMKLocationService alloc]init];
        //开始定位
        [_locService startUserLocationService];
        _locService.delegate = self;
    }
    
#endif
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.automaticallyAdjustsScrollViewInsets = NO ;
    [self getProData];
    _bigAreaDictionary = [[NSMutableDictionary alloc]init];
    _headView = [[UIView alloc]initWithFrame:CGRectMake(0, 0,mainScreen_Width , 64 * suitableScreen_Width)];
    [_headView setBackgroundColor:[UIColor colorWithRed:0.088 green:0.331 blue:1.000 alpha:1.000]];
    [self.view addSubview:_headView];
    
    UILabel *titleLab = [[UILabel alloc]initWithFrame:CGRectMake(mainScreen_Width / 2.542 - 20 * suitableScreen_Width, mainScree_Height / 24.703, 210 * suitableScreen_Width, mainScree_Height / 22.233)];
    [_headView addSubview:titleLab];
    [titleLab setText:@"汽  车  详  情"];
   
    [titleLab setTextColor:[UIColor whiteColor]];
    UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [backButton setImage:[UIImage imageNamed:@"iconfont-fanhui"] forState:UIControlStateNormal];
    [backButton addTarget:self action:@selector(backAction:) forControlEvents:UIControlEventTouchUpInside];
    [_headView addSubview:backButton];
    [backButton setFrame:CGRectMake(mainScreen_Width / 22.058, mainScree_Height / 24.703, mainScreen_Width / 12.5, mainScree_Height / 22.233)];
    [backButton setTintColor:[UIColor redColor]];
    [backButton setTitleColor:[UIColor colorWithRed:0.088 green:0.331 blue:1.000 alpha:1.000] forState:UIControlStateSelected];
    [self createView];
    _proTag = 0 ;
    _cityTag = 0 ;
#pragma mark - collection点击详情
#pragma mark - 数据解析
    [ AFNblock getdataWithString:[NSString stringWithFormat:@"http://app.api.autohome.com.cn/autov4.8.8/cars/seriessummary-pm1-s%@-t-c130100.json",self.model.cid]  block:^(id block) {
        NSMutableArray *mutableArray = [[NSMutableArray alloc]init];
        [mutableArray addObject:[block objectForKey:@"result"]];
        for (NSDictionary *dic in mutableArray) {
            CCFindHeadDetailModel * model = [CCFindHeadDetailModel modelWithDictionary:dic];
            
            UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, mainScreen_Width, 330 *suitableScreen_Width)];
            [self.view addSubview:view];
            
            
            UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, mainScreen_Width, 260 * suitableScreen_Width)];
            [imageView sd_setImageWithURL:[NSURL URLWithString:model.logo]placeholderImage:[UIImage imageNamed:@"zhanweitu"]];
            [imageView setUserInteractionEnabled:YES];
            [view addSubview:imageView];
            
            
            self.tableView.tableHeaderView = view;
            UILabel *nameLabel = [[UILabel alloc]initWithFrame:CGRectMake(15  * suitableScreen_Width, 270 * suitableScreen_Width, mainScreen_Width, 25 * suitableScreen_Width)];
            nameLabel.textAlignment = NSTextAlignmentLeft;
            nameLabel.textColor = [UIColor blackColor];
            nameLabel.text = [NSString stringWithFormat:@"%@      %@",model.name,model.levelname];
            nameLabel.font = [UIFont systemFontOfSize:16.];
            [view addSubview:nameLabel];
            
            UILabel *factoary = [[UILabel alloc]initWithFrame:CGRectMake(15 * suitableScreen_Width, 300 * suitableScreen_Width, mainScreen_Width - 15 * suitableScreen_Width, 25 * suitableScreen_Width)];
            factoary.textAlignment = NSTextAlignmentLeft;
            factoary.textColor = [UIColor grayColor];
            factoary.font = [UIFont systemFontOfSize:15.];
            factoary.text = [NSString stringWithFormat:@"厂商指导价 : %@",model.fctprice];
            [view addSubview:factoary];
            
            NSInteger length = model.fctprice.length;
            NSMutableAttributedString *mutableString = [[NSMutableAttributedString alloc]initWithString:factoary.text];
            [mutableString addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(8, length )];
            factoary.attributedText = mutableString;
            
            _sectionArray = [[NSMutableArray alloc]init];
            _allCarArray = [[NSMutableArray alloc]init];
            
            for (NSDictionary *ddd in [dic objectForKey:@"enginelist"]) {
                CCFindHeadDetailModel *model = [CCFindHeadDetailModel modelWithDictionary:ddd];
                
                [_sectionArray addObject:model];
                NSMutableDictionary *mutableDic = [[NSMutableDictionary alloc]init];
                [_allCarArray addObject:mutableDic];
                
                NSMutableArray *mutableArray = [[NSMutableArray alloc]init];
                for (NSDictionary *dict in [ddd objectForKey:@"speclist"]) {
                    CCFindHeadDetailModel * model = [CCFindHeadDetailModel modelWithDictionary:dict];
                    
                    model.titleName = [dict objectForKey:@"name"];
                    
                    [mutableArray addObject:model];
                }
                [mutableDic setObject:mutableArray forKey:@"carsArray"];
            }
            [self.tableView reloadData];
        }
        
    }];
    
    
    
}
#pragma mark - 在地图View将要启动定位时,会调用此函数
- (void)willStartLocatingUser{
    NSLog(@"start locate");
}
#pragma mark - 用户位置更新后,会调用此函数
//userLocation:新的用户位置
- (void)didUpdateBMKUserLocation:(BMKUserLocation *)userLocation{
    
    _lat = userLocation.location.coordinate.latitude ;
    _lon = userLocation.location.coordinate.longitude ;
    NSLog(@"------------------%f,%f",_lat,_lon);
    
    //动态更新位置坐标
    [_mapView updateLocationData:userLocation];
   
    //初始化检索对象
    _searcher =[[BMKGeoCodeSearch alloc]init];
    _searcher.delegate = self;
    //发起反向地理编码检索
    CLLocationCoordinate2D pt = (CLLocationCoordinate2D){userLocation.location.coordinate.latitude, userLocation.location.coordinate.longitude};
    BMKReverseGeoCodeOption *reverseGeoCodeSearchOption = [[BMKReverseGeoCodeOption alloc]init];
    reverseGeoCodeSearchOption.reverseGeoPoint = pt;
    BOOL flag = [_searcher reverseGeoCode:reverseGeoCodeSearchOption];
    
    if(flag)
    {
        //  NSLog(@"反geo检索发送成功");
        
    }
    else
    {
        // NSLog(@"反geo检索发送失败");
    }
    
    
}
- (void)onGetReverseGeoCodeResult:(BMKGeoCodeSearch *)searcher result:(BMKReverseGeoCodeResult *)result errorCode:(BMKSearchErrorCode)error
{
    
    NSString * strCity = result.addressDetail.city;
    if (![strCity isEqualToString: @""]) {
        [[NSUserDefaults standardUserDefaults]setObject:@"distance" forKey:@"distance"];
        NSString * cityPlace = [strCity stringByReplacingOccurrencesOfString:@"市" withString:@""];
        [_areaButton setTitle:cityPlace forState:UIControlStateNormal ];
        [[NSUserDefaults standardUserDefaults]setObject:cityPlace forKey:@"city"];
        [[NSUserDefaults standardUserDefaults]setObject:@"area" forKey:@"area"];
        [self.tableView reloadData];
    }else{
        [_locService stopUserLocationService];
        //不显示距离
        [[NSUserDefaults standardUserDefaults]setObject:@"notdistance" forKey:@"distance"];
        UIAlertController * alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"定位失败,请手动选取要查看的地区" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction * cancelAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:nil
                                        ];
        [alert addAction:cancelAction];
        [self presentViewController:alert animated:YES completion:^{
            
        }];
    }
}
#pragma mark - 在地图的View停止定位后,调用此函数
- (void)didStopLocatingUser{
    NSLog(@"stop locate");
}
#pragma mark - 定位失败调用此函数
- (void)didFailToLocateUserWithError:(NSError *)error{
    NSLog(@"location error");
    //定位失败时把开关关上
    [[NSUserDefaults standardUserDefaults]setObject:@"area" forKey:@"notarea"];
    //不显示距离
    [[NSUserDefaults standardUserDefaults]setObject:@"notdistance" forKey:@"distance"];
    [_locService stopUserLocationService];
    UIAlertController * alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"定位失败,请在右上角选取要查看的地区" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction * cancelAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:nil
                                    ];
    [alert addAction:cancelAction];
    [self presentViewController:alert animated:YES completion:^{
        
    }];
    
    
}

- (void)backAction:(UIButton *)backBut
{
    [_locService stopUserLocationService];
    self.navigationController.navigationBar.hidden = NO;
    [self.navigationController popViewControllerAnimated:YES];
    [_cityTableView setFrame:CGRectMake(mainScreen_Width, 0 * suitableScreen_Width, mainScreen_Width/2 , mainScree_Height) ];
    [_provinceTableView setFrame:CGRectMake(mainScreen_Width, 0 * suitableScreen_Width, mainScreen_Width/2 , mainScree_Height)];
    
}
- (void)createView
{
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0,64 * suitableScreen_Width, mainScreen_Width, mainScree_Height - 64 * suitableScreen_Width ) style:UITableViewStylePlain];
    self.tableView.delegate =self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
    self.title = @"" ;
#pragma mark - 地区按钮
    _areaButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [_areaButton setFrame:CGRectMake(300 * suitableScreen_Width, 30 * suitableScreen_Width, 70 * suitableScreen_Width, 30 * suitableScreen_Width)];
    _areaButton.alpha = 0.7 ;
    [_areaButton setTitle:@"地区" forState:UIControlStateNormal];
#pragma mark - 点击cityTableView得到的button文字
    if ([[[NSUserDefaults standardUserDefaults]objectForKey:@"area"]isEqualToString:@"area"]) {
        
        NSString * strC = [[NSUserDefaults standardUserDefaults]objectForKey:@"city"];
        [_areaButton setTitle:strC forState:UIControlStateNormal];
    }
    
    [_areaButton.titleLabel setFont:[UIFont systemFontOfSize:14]];
    [_headView addSubview:_areaButton];
    _areaButton.hidden = NO ;
    [_areaButton addTarget:self action:@selector(pushArea) forControlEvents:UIControlEventTouchUpInside];
    
#pragma mark - 省tableView
    
    _provinceTableView = [[UITableView alloc]initWithFrame:CGRectMake(mainScreen_Width, 0 * suitableScreen_Width, mainScreen_Width/2 , mainScree_Height) style:UITableViewStylePlain];
    [self.view addSubview:_provinceTableView];
    
    _provinceTableView.delegate = self ;
    _provinceTableView.dataSource = self ;
    
    UIView * proView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, mainScreen_Width/3, 70 * suitableScreen_Width)];
    _provinceTableView.tableHeaderView = proView ;
    
    UILabel * proTitleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0 * suitableScreen_Width, mainScreen_Width / 6, 32 * suitableScreen_Width)];
    [proView addSubview:proTitleLabel];
    [proTitleLabel setText:@"当前省"];
    _pLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 32 * suitableScreen_Width, mainScreen_Width / 6, 32 * suitableScreen_Width)];
    _pLabel.layer.borderColor = [[UIColor lightGrayColor]CGColor] ;
    _pLabel.layer.cornerRadius = 5 ;
    _pLabel.layer.borderWidth = 2 ;
    _pLabel.layer.masksToBounds = YES;
    _pLabel.layer.borderColor = [[UIColor colorWithRed:246/255. green:246/255. blue:246/255. alpha:1]CGColor] ;
    [_pLabel setTextAlignment:NSTextAlignmentCenter];
    [proView addSubview:_pLabel];
    
    NSString * proString = [[NSUserDefaults standardUserDefaults]objectForKey:@"pro"];
    
    [_pLabel setText:proString];
    UIView * pView = [[UIView alloc]initWithFrame:CGRectMake(0, 67 * suitableScreen_Width, mainScreen_Width / 3, 3 * suitableScreen_Width)];
    pView.alpha = 0.3 ;
    [pView setBackgroundColor:[UIColor colorWithRed:0.088 green:0.331 blue:1.000 alpha:1.000]];    [proView addSubview:pView];
    
#pragma mark - 城市tableView
    _cityTableView = [[UITableView alloc]initWithFrame: CGRectMake(mainScreen_Width, 0 * suitableScreen_Width, mainScreen_Width/2 , mainScree_Height) style:UITableViewStylePlain];
    _cityTableView.delegate = self ;
    
    _cityTableView.dataSource = self ;
    UIView * cityView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, mainScreen_Width/3, 70 * suitableScreen_Width)];
    _cityTableView.tableHeaderView = cityView ;        [self.view addSubview:_cityTableView];
    UILabel * cityTitleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0 * suitableScreen_Width, mainScreen_Width / 6, 32 * suitableScreen_Width)];
    [cityView addSubview:cityTitleLabel];
    [cityTitleLabel setText:@"当前市"];
    _cLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 32 * suitableScreen_Width, mainScreen_Width / 6, 32 * suitableScreen_Width)];
    [cityView addSubview:_cLabel];
    [_cLabel setTextAlignment:NSTextAlignmentCenter];
    _cLabel.layer.borderColor = [[UIColor colorWithRed:246/255. green:246/255. blue:246/255. alpha:1]CGColor] ;
    
    _cLabel.layer.cornerRadius = 5 ;
    _cLabel.layer.borderWidth = 2;
    _cLabel.layer.masksToBounds = YES;
    NSString * cityString = [[NSUserDefaults standardUserDefaults]objectForKey:@"city"];
    [_cLabel setText:cityString];
    UIView * cView = [[UIView alloc]initWithFrame:CGRectMake(0, 67 * suitableScreen_Width, mainScreen_Width / 3, 3 * suitableScreen_Width)];
    cView.alpha = 0.3 ;
    [cView setBackgroundColor:[UIColor colorWithRed:0.088 green:0.331 blue:1.000 alpha:1.000]];
    [cityView addSubview:cView];
    
}
#pragma mark - 省数据

-(void)getProData{
    _proArray = [[NSMutableArray alloc]init];
    [AFNblock getdataWithString:@"http://app.api.autohome.com.cn/autov5.0.0/news/province-pm2-ts0.json" block:^(id block) {
        
        NSMutableDictionary * reDic = [block objectForKey:@"result"];
        
        
        NSMutableArray * proArr = [reDic objectForKey:@"provinces"];
        for (NSMutableDictionary * proDic in proArr) {
            CCProFindModel * model = [CCProFindModel modelWithDictionary:proDic];
            [_proArray addObject:model];
            NSMutableArray * cityA = [proDic objectForKey:@"citys"];
            for (NSMutableDictionary * dict in cityA) {
                
#pragma mark - 所有的市名和id都装进大字典里
                NSString * nameStr = [dict objectForKey:@"name"];
                NSNumber * cidNumber = [dict objectForKey:@"id"];
                
                [_bigAreaDictionary setObject:cidNumber forKey:nameStr];
                
            }
        }
        [_provinceTableView reloadData];
        [_cityTableView reloadData];
    }];
    
}
#pragma mark - 省tableview推出来
-(void)pushArea{
    [UIView animateWithDuration:0.3 animations:^{
        [_provinceTableView setFrame:CGRectMake(mainScreen_Width * 2/ 3, 0 , mainScreen_Width  /3, mainScree_Height)];
    }];
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.0000000000000000001 ;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    if (tableView == self.tableView) {
        
        
        return _sectionArray.count;
    }
    return 1 ;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    if (tableView == self.tableView) {
        
        CCFindHeadDetailModel *model = [_sectionArray objectAtIndex:section];
        return model.name;
    }
    return nil ;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView == self.tableView) {
        return 100 * suitableScreen_Width;
    }
    return 40 ;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (tableView == self.tableView) {
        
        
        return [[[_allCarArray objectAtIndex:section]objectForKey:@"carsArray"] count];
    }else if (tableView == _provinceTableView){
        return _proArray.count ;
    }else if (tableView == _cityTableView){
        return _cityArray.count ;
    }
    return 0 ;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([tableView isEqual:self.tableView]) {
        
        static NSString *cellId = @"cell";
        CCfindHeadDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
        if (!cell) {
            cell = [[CCfindHeadDetailCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
        }
        if (_allCarArray.count!=0) {
            
            CCFindHeadDetailModel *model = [[[_allCarArray objectAtIndex:[indexPath section]]objectForKey:@"carsArray"]objectAtIndex:indexPath.row];
            [cell getInfo:model];
        }
        return cell;
    }else if([tableView isEqual:_provinceTableView]){
        static NSString * cellID = @"cellPro" ;
        
        UITableViewCell * cellPro = [tableView dequeueReusableCellWithIdentifier:cellID];
        
        if (!cellPro) {
            
            cellPro = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
            
        }
        if (_proArray.count!=0) {
            CCProFindModel * model =[_proArray objectAtIndex:indexPath.row];
            [cellPro.textLabel setText:model.name];
        }
        UISwipeGestureRecognizer * swipPro = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(proRight)];
        [swipPro setDirection:UISwipeGestureRecognizerDirectionRight];
        [cellPro addGestureRecognizer:swipPro];
        [_provinceTableView addGestureRecognizer:swipPro];
        return cellPro ;
    }else if (tableView == _cityTableView){
        
        static NSString * cellIC = @"cellCT";
        
        UITableViewCell * cellCT = [tableView dequeueReusableCellWithIdentifier:cellIC];
        if (!cellCT) {
            
            cellCT = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIC];
        }
        if (_cityArray.count!=0) {
            NSMutableDictionary * cityDic =[_cityArray objectAtIndex:indexPath.row];
            [cellCT.textLabel setText:[cityDic objectForKey:@"name"]];
        }
        UISwipeGestureRecognizer * swipCity = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(cityRight)];
        [swipCity setDirection:UISwipeGestureRecognizerDirectionRight];
        [cellCT addGestureRecognizer:swipCity];
        [_cityTableView addGestureRecognizer:swipCity];
        return cellCT ;
    }
    
    return nil ;
}
#pragma mark - 市手势
-(void)cityRight{
    [UIView animateWithDuration:0.3 animations:^{
        [_cityTableView setFrame:CGRectMake(mainScreen_Width , 0, mainScreen_Width /3, mainScree_Height)];
    }];
}
#pragma mark - 省手势
-(void)proRight{
    
    [UIView animateWithDuration:0.3 animations:^{
        [_provinceTableView setFrame:CGRectMake(mainScreen_Width , 0, mainScreen_Width /3, mainScree_Height)];
    }];
    
}
#pragma mark - 点击方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView == self.tableView) {
        
        [_cityTableView setFrame:CGRectMake(mainScreen_Width, 0 * suitableScreen_Width, mainScreen_Width/2 , mainScree_Height) ];
        [_provinceTableView setFrame:CGRectMake(mainScreen_Width, 0 * suitableScreen_Width, mainScreen_Width/2 , mainScree_Height)];
        _i = 0 ;
        for (NSString * cityK in _bigAreaDictionary.allKeys) {
            
            if ([cityK isEqualToString:_areaButton.titleLabel.text]) {
                _i = _i + 1 ;
            }
        }
        if (_i != 0 ) {
            
            CCTypePriceViewController * typeVc = [[CCTypePriceViewController alloc]init];
            
            typeVc.lat = _lat ;
            typeVc.lon = _lon ;
            typeVc.model = [[[_allCarArray objectAtIndex:[indexPath section]]objectForKey:@"carsArray"]objectAtIndex:indexPath.row];
            
            typeVc.hidesBottomBarWhenPushed = YES ;
            NSString * cityKey = [[NSUserDefaults standardUserDefaults]objectForKey:@"city"];
            typeVc.cid = [_bigAreaDictionary objectForKey:cityKey];
            [self.navigationController pushViewController:typeVc animated:YES];
        }else{
            UIAlertController * alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"请在右上角选取要查看的地区" preferredStyle:UIAlertControllerStyleAlert];
            UIAlertAction * cancelAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:nil
                                            ];
            [alert addAction:cancelAction];
            [self presentViewController:alert animated:YES completion:^{
                
            }];
            
            
        }
    }else if (tableView == _provinceTableView){
        
        _proTag = indexPath.row ;
        CCProFindModel * model = [_proArray objectAtIndex:_proTag];
        _cityArray = [NSMutableArray arrayWithArray:model.citys];
        [UIView animateWithDuration:0.3 animations:^{
            [_cityTableView setFrame:CGRectMake(mainScreen_Width * 2/ 3, 0, mainScreen_Width, mainScree_Height)];
            [_provinceTableView setFrame:CGRectMake(mainScreen_Width, 0, mainScreen_Width / 3, mainScree_Height)];
        }];
        _proText = model.name ;
        [_pLabel setText:_proText];
#pragma mark - 轻量级存储当前省
        [[NSUserDefaults standardUserDefaults]setObject:_proText forKey:@"pro"];
        [_cityTableView reloadData];
        
    }else if (tableView == _cityTableView){
        [_locService stopUserLocationService];
        _cityTag = indexPath.row ;
        NSMutableDictionary * cityDict = [_cityArray objectAtIndex:indexPath.row];
        _cid = [cityDict objectForKey:@"id"];
        _cityText = [cityDict objectForKey:@"name"];
        [_cLabel setText:_cityText];
        [_areaButton setTitle:_cityText forState:UIControlStateNormal];
#pragma mark - 轻量级存储当前城市
        [[NSUserDefaults standardUserDefaults]setObject:_cityText forKey:@"city"];
#pragma mark - 地区开关文字判断
        [[NSUserDefaults standardUserDefaults]setObject:@"area" forKey:@"area"];
        [_cityTableView reloadData];
        
        [self.tableView reloadData];
        [UIView animateWithDuration:0.3 animations:^{
            
            [_cityTableView setFrame:CGRectMake(mainScreen_Width, 0, mainScreen_Width / 3, mainScree_Height)];
        }];
        
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
